﻿Public Class MatrixOperations

    Public Function matrixInvert(ByVal input(,) As Double) As Double(,)
        If input.GetLength(0) <> input.GetLength(1) Then
            Throw New System.Exception("An exception has occurred.Size of matrices do not comply")
            Exit Function
        End If
        Dim calcMat(input.GetLength(0) - 1, 2 * input.GetLength(1) - 1) As Double

        For i As Integer = 0 To calcMat.GetLength(0) - 1
            For j As Integer = 0 To calcMat.GetLength(1) - 1
                If j > input.GetLength(1) - 1 Then
                    If j = i + input.GetLength(1) Then
                        calcMat(i, j) = 1
                    Else
                        calcMat(i, j) = 0
                    End If
                Else
                    calcMat(i, j) = input(i, j)
                End If
            Next
        Next

        For i As Integer = 0 To input.GetLength(0) - 1

            For j As Integer = 0 To calcMat.GetLength(1) - 1
                If i <> j Then
                    calcMat(i, j) = calcMat(i, j) / calcMat(i, i)
                End If
            Next

            calcMat(i, i) = 1 ' set explicitly otherwise if set in upper loop it will not change other values ...apply logic

            For k As Integer = 0 To input.GetLength(0) - 1
                If i <> k Then
                    Dim mulFactor As Double = calcMat(k, i)
                    For l As Integer = 0 To calcMat.GetLength(1) - 1
                        calcMat(k, l) = calcMat(k, l) - mulFactor * calcMat(i, l)
                    Next
                End If
            Next
        Next

        Dim invertedMat(input.GetLength(0) - 1, input.GetLength(1) - 1) As Double
        For i As Integer = 0 To input.GetLength(0) - 1
            For j As Integer = 0 To input.GetLength(1) - 1
                invertedMat(i, j) = calcMat(i, j + input.GetLength(1))
                'MsgBox(invertedMat(i, j))
            Next
        Next

        Return invertedMat
    End Function


    Public Function matrixSum(ByVal in1(,) As Double, ByVal in2(,) As Double) As Double(,)
        Dim sum(in1.GetLength(0) - 1, in1.GetLength(1) - 1) As Double
        For i As Integer = 0 To in1.GetLength(0) - 2
            For j As Integer = 0 To in1.GetLength(1) - 2
                sum(i, j) = in1(i, j) + in2(i, j)
            Next
        Next
        Return sum
    End Function


    Public Function vactorSum(ByVal in1() As Double, ByVal in2() As Double) As Double()
        Dim sum(in1.GetLength(0) - 1) As Double
        For i As Integer = 0 To in1.GetLength(0) - 2
            sum(i) = in1(i) + in2(i)
        Next
        Return sum
    End Function

    Public Function matrixTranspose(ByVal in1(,) As Double) As Double(,)
        Dim transpose(in1.GetLength(1) - 1, in1.GetLength(0) - 1) As Double
        For i As Integer = 0 To in1.GetUpperBound(0)
            For j As Integer = 0 To in1.GetUpperBound(1)
                transpose(j, i) = in1(i, j)
            Next
        Next
        Return transpose
    End Function

    Public Function matrixMultiplication(ByVal in1(,) As Double, ByVal in2(,) As Double) As Double(,)
        If in1.GetLength(1) <> in2.GetLength(0) Then
            Throw New System.Exception("An exception has occurred.Size of matrices do not comply")
        Else
            Dim multiply(in1.GetLength(0) - 1, in2.GetLength(1) - 1) As Double
            For i As Integer = 0 To in1.GetLength(0) - 1
                For j As Integer = 0 To in2.GetLength(1) - 1
                    Dim sum As Double = 0
                    For k As Integer = 0 To in1.GetLength(1) - 1
                        sum += in1(i, k) * in2(k, j)
                    Next
                    multiply(i, j) = sum
                Next
            Next
            Return multiply
        End If
    End Function

    Public Function matrixVectorMultiplication(ByVal in1(,) As Double, ByVal in2() As Double) As Double(,)
        Dim temp(in2.GetLength(0) - 1, 0) As Double
        For i = 0 To in2.GetLength(0) - 1
            For j = 0 To 0
                temp(i, j) = in2(i)
            Next
        Next

        If temp.GetLength(0) <> in1.GetLength(1) Then
            Throw New System.Exception("An exception has occurred.Size of matrices do not comply")

        Else
            Dim multiply(in1.GetLength(0) - 1, temp.GetLength(1) - 1) As Double
            For i As Integer = 0 To in1.GetLength(0) - 1
                For j As Integer = 0 To temp.GetLength(1) - 1
                    Dim sum As Double = 0
                    For k As Integer = 0 To temp.GetLength(0) - 1
                        sum += in1(i, k) * temp(k, j)
                    Next
                    multiply(i, j) = sum
                Next

            Next
            Return multiply
        End If
    End Function

    Public Function matrixConstantMultiplication(ByVal constant As Double, ByVal input(,) As Double) As Double(,)

        For i As Integer = 0 To input.GetLength(0) - 1
            For j As Integer = 0 To input.GetLength(1) - 1
                input(i, j) = constant * input(i, j)
            Next
        Next
        Return input
    End Function


    Public Function factorial(ByRef num As Double) As Double
        Dim fact As Double = 1
        For i As Integer = num To 1 Step -1
            fact = fact * i
        Next
        Return fact
    End Function
End Class
